import { exec } from 'child_process';

class WindowsLocker {
    private isLocked: boolean;

    constructor() {
        this.isLocked = false;
    }

    async enableLock(): Promise<void> {
        if (this.isLocked) return;
        console.log('🪟 启用 Windows 系统锁定');

        try {
            const commands: string[] = [
                // 禁用任务管理器
                'REG add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System /v DisableTaskMgr /t REG_DWORD /d 1 /f',

                // 禁用运行对话框
                'REG add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer /v NoRun /t REG_DWORD /d 1 /f',

                // 禁用开始菜单
                'REG add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer /v NoStartMenu /t REG_DWORD /d 1 /f',

                // 禁用任务栏
                'REG add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer /v NoTaskbar /t REG_DWORD /d 1 /f',

                // 禁用 Alt+Tab
                'REG add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer /v NoAltTab /t REG_DWORD /d 1 /f',

                // 禁用 Win 键
                'REG add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer /v NoWinKeys /t REG_DWORD /d 1 /f'
            ];

            for (const cmd of commands) {
                await this.executeCommand(cmd);
            }

            this.isLocked = true;
            console.log('✅ Windows 系统锁定已启用');
        } catch (error) {
            console.error('❌ Windows 锁定失败:', error);
        }
    }

    async disableLock(): Promise<void> {
        if (!this.isLocked) return;
        console.log('🪟 禁用 Windows 系统锁定');

        try {
            const commands: string[] = [
                'REG add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System /v DisableTaskMgr /t REG_DWORD /d 0 /f',
                'REG add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer /v NoRun /t REG_DWORD /d 0 /f',
                'REG add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer /v NoStartMenu /t REG_DWORD /d 0 /f',
                'REG add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer /v NoTaskbar /t REG_DWORD /d 0 /f',
                'REG add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer /v NoAltTab /t REG_DWORD /d 0 /f',
                'REG add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer /v NoWinKeys /t REG_DWORD /d 0 /f'
            ];

            for (const cmd of commands) {
                await this.executeCommand(cmd);
            }

            this.isLocked = false;
            console.log('✅ Windows 系统锁定已禁用');
        } catch (error) {
            console.error('❌ Windows 解锁失败:', error);
        }
    }

    private executeCommand(command: string): Promise<void> {
        return new Promise((resolve) => {
            exec(command, (error) => {
                if (error) {
                    console.log(`命令执行警告: ${command} - ${error.message}`);
                }
                resolve();
            });
        });
    }
}

export default WindowsLocker;